package com.gym.code.config.admin.client;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gym.code.config.admin.client.model.DBDatabaseModel;

/**
 * 
 * @author xinchun.wang
 * @email: 532002108@qq.com
 * @createTime 2015-4-19 下午4:34:59
 */
public class DBUtils {
	private static final Logger logger = LoggerFactory.getLogger(DBUtils.class);

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			logger.error("", e);
			System.exit(0);
		}
	}

	public static Connection getConnection(DBDatabaseModel dbModel) {
		String url = "jdbc:mysql://" + dbModel.getIp() + ":" + dbModel.getPort() + "/" + dbModel.getDbName()+"?";
		try {
			Connection conn = DriverManager.getConnection(url, dbModel.getUsername(), dbModel.getPassword());
			return conn;
		} catch (SQLException e) {
			logger.error("",e);
		}
		return null;
	}
	
	
	public static void closeConnection(Connection conn){
		try {
			if(conn == null || conn.isClosed()){
				return;
			}
			conn.close();
		} catch (SQLException e) {
			logger.error("",e);
		}
	}
	
	
	public  static void closeStat(Statement stat){
		try {
			if(stat == null || stat.isClosed()){
				return;
			}
			stat.close();
		} catch (SQLException e) {
			logger.error("",e);
		}
	}
}
